﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public class WaitLoadAssetBundleAsync : CustomYieldInstruction
    {
        private AssetBundleLoadTask abLoadTask;
        private AssetBundle assetbundle;
        private WaitForSecondsRealtime waitTime;

        public WaitLoadAssetBundleAsync(AssetBundleLoadTask task)
        {
            this.abLoadTask = task;
        }

        public WaitLoadAssetBundleAsync(AssetBundle ab)
        {
            this.assetbundle = ab;
        }

        public WaitLoadAssetBundleAsync(WaitForSecondsRealtime time)
        {
            this.waitTime = time;
        }

        public override bool keepWaiting
        {
            get
            {
                if (this.waitTime != null) return this.waitTime.keepWaiting;
                return Error == null && AssetBundle == null;
            }
        }

        public string Error
        {
            get
            {
                if (this.waitTime != null)
                {
                    return (!this.waitTime.keepWaiting) ? "Simulate update failed" : string.Empty;
                }
                if (this.abLoadTask != null)
                {
                    return abLoadTask.Error;
                }
                return null;
            }
        }

        public AssetBundle AssetBundle
        {
            get
            {
                if (this.waitTime != null) return null;
                if (this.assetbundle != null) return this.assetbundle;
                return this.abLoadTask.AssetBundle;
            }
        }

    }
}


